package com.ivo.quartz;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ivo.biz.PerformanceService;
import com.ivo.sys.Code;
import com.ivo.sys.CodeService;
import com.ivo.util.DBUtil;


/**
 * 
 *
 * @author Minkey Wang
 * @version 2017年7月17日
 */
@Repository
public class AuditDataQuartz {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
    @Autowired
    private PerformanceService performanceService;
    
    @Autowired
    private CodeService codeService;
    
    @Transactional
    @Scheduled(cron = "0 25 6 16 * ? ")//每月16日上午06:25触发
	public void execute() throws ParseException {
    	
    	logger.info("同步稽核数据start");
    	Calendar calendar = Calendar.getInstance();
		SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
		String str = sdf.format(calendar.getTime());
		String month = str.substring(0, 7);
		String todate =month+"-11";
		calendar.add(Calendar.MONTH, -1);    //得到前一个月   
		String str1 = sdf.format(calendar.getTime());
		String month1 = str1.substring(0, 7);
		String fromdate =month1+"-10";
    	
//    	String fromdate ="2017-08-17";
//    	String todate   ="2017-08-28";
    	
    	performanceService.deleteByDayByFabdate(fromdate,todate);
    	
		String sql = "select 'CEL' factory, '稽核管理' category,fabDate,station,missingLevel,missingType,responsibleUnit,engineerName,"
				+ " auditedPersonName,auditedPersonJobNumber,abnormalDescription from Internal_Audit_Inspection"
				+ " where CONVERT(varchar(100), fabDate, 23) >='"+fromdate+"' and CONVERT(varchar(100), fabDate, 23) <='"+todate+"' "
				+ "and auditedPersonName is not null and auditedPersonName<>'' and auditedPersonJobNumber<>'' ";
		DBUtil db = new DBUtil();
		ResultSet rs =db.executeQueryRS(sql);
		try {
			while(rs.next()){
				String factory = rs.getString("factory");
				String category = rs.getString("category");
				String fabDate = rs.getString("fabDate");
				Long type = (long) 23;
				String ope_name = rs.getString("station");
				Long ope_id = (long) -1;
				Code code = codeService.getCELCodeByLabel(ope_name);
				if(code!=null) ope_id= code.getId();
				String emp_id = rs.getString("auditedPersonJobNumber").trim();
				if(emp_id.length()>8) emp_id=emp_id.substring(0, 7);
				String emp_name = rs.getString("auditedPersonName");
				String group_leader = rs.getString("engineerName");
				String class_type="";
				String reason= rs.getString("abnormalDescription");
				
				
				String id = performanceService.createByDay(factory,category,fabDate,type,ope_id,emp_id,emp_name,group_leader,class_type,reason);
//				System.out.println(id);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		logger.info("同步稽核数据end");
    }
}
